package com.lb.videoplay.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.blankj.utilcode.util.ActivityUtils;
import com.lb.videoplay.R;
import com.lb.videoplay.statuslib.StatusUtil;

import java.util.ArrayList;
import java.util.List;
/**
 * Created by Mr.lb
 * 时间： 2018/11/15
 * 包名： com.lb.videoplay.activity
 * 描述： 指引页面
 */
public class GuideActivity extends Activity {

    private ViewPager mViewPager;
    private Button mBtConfirm;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initWidget();
        initEvent();
        initData();
    }

    public void initData() {
        // 第二个参数是是否沉浸,第三个参数是状态栏字体是否为黑色
        StatusUtil.setSystemStatus(this, true, true);
        addData();
        MyPagerAdapter adapter = new MyPagerAdapter();
        mViewPager.setAdapter(adapter);
        mViewPager.setOnPageChangeListener(new OnMyPageChangeListener());
    }

    public void initEvent() {
        mBtConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.startActivity(MainActivity.class);
                ActivityUtils.finishActivity(GuideActivity.this);
            }
        });
    }

    /**
     * 初始化控件
     */
    public void initWidget() {
        mViewPager = findViewById(R.id.view_pager);
        mBtConfirm = findViewById(R.id.bt_confirm);
    }

    /**
     * 数据源
     */
    private List<Integer> imageResList = new ArrayList<Integer>();

    private void addData() {
        imageResList.add(R.drawable.leadpage_one);
        imageResList.add(R.drawable.leadpage_two);
        imageResList.add(R.drawable.leadpage_three);
    }

    private final class OnMyPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageSelected(int position) {
            //界面改变时，显示按钮
            if (position == imageResList.size() - 1) {
                mBtConfirm.setVisibility(View.VISIBLE);
            } else {
                mBtConfirm.setVisibility(View.GONE);
            }
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageResList.size();
        }

        /**
         * 1.根据当前position获取对应的view对象,添加到container内b
         * 2.将view对象返回
         */
        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            //根据位置获取对应的图片资源
            Integer currentImgageResID = imageResList.get(position);
            ImageView imageView = new ImageView(GuideActivity.this);
            imageView.setImageResource(currentImgageResID);
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);//拉伸图片跟ImageView的大小一致
            container.addView(imageView);
            return imageView;
        }

        /*
         * 将Object从container移除
         */
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);//Object就是instantateItem返回的imageview
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            ActivityUtils.finishAllActivities();
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}
